<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="SSM," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="岁月极美,在于它必然的流逝。春花,秋月,夏日,冬雪。你若盛开,清风自来。佛是过来人，人是未来佛。  @RequestMapping value的值是数组，可以将多个url映射到同一个方法1234567@RequestMapping(value = &amp;#123; &quot;itemList&quot;, &quot;itemListAll&quot; &amp;#125;)``` - 在class上添加@RequestMapping(url)">
<meta name="keywords" content="SSM">
<meta property="og:type" content="article">
<meta property="og:title" content="ssm-2">
<meta property="og:url" content="http://yoursite.com/2017/06/14/ssm-2/index.html">
<meta property="og:site_name" content="JokerLiang">
<meta property="og:description" content="岁月极美,在于它必然的流逝。春花,秋月,夏日,冬雪。你若盛开,清风自来。佛是过来人，人是未来佛。  @RequestMapping value的值是数组，可以将多个url映射到同一个方法1234567@RequestMapping(value = &amp;#123; &quot;itemList&quot;, &quot;itemListAll&quot; &amp;#125;)``` - 在class上添加@RequestMapping(url)">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-06-14T07:27:02.979Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ssm-2">
<meta name="twitter:description" content="岁月极美,在于它必然的流逝。春花,秋月,夏日,冬雪。你若盛开,清风自来。佛是过来人，人是未来佛。  @RequestMapping value的值是数组，可以将多个url映射到同一个方法1234567@RequestMapping(value = &amp;#123; &quot;itemList&quot;, &quot;itemListAll&quot; &amp;#125;)``` - 在class上添加@RequestMapping(url)">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"always"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/06/14/ssm-2/"/>





  <title> ssm-2 | JokerLiang </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  








  <div style="display: none;">
    <script src="//s95.cnzz.com/z_stat.php?id=1260895995&web_id=1260895995" language="JavaScript"></script>
  </div>





  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">JokerLiang</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">永远年轻，永远热泪盈眶。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/06/14/ssm-2/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="JokerLiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/me.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="JokerLiang">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="JokerLiang" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                ssm-2
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-06-14T15:19:17+08:00">
                2017-06-14
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/SSM/" itemprop="url" rel="index">
                    <span itemprop="name">SSM</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>&#x5C81;&#x6708;&#x6781;&#x7F8E;,&#x5728;&#x4E8E;&#x5B83;&#x5FC5;&#x7136;&#x7684;&#x6D41;&#x901D;&#x3002;&#x6625;&#x82B1;,&#x79CB;&#x6708;,&#x590F;&#x65E5;,&#x51AC;&#x96EA;&#x3002;&#x4F60;&#x82E5;&#x76DB;&#x5F00;,&#x6E05;&#x98CE;&#x81EA;&#x6765;&#x3002;&#x4F5B;&#x662F;&#x8FC7;&#x6765;&#x4EBA;&#xFF0C;&#x4EBA;&#x662F;&#x672A;&#x6765;&#x4F5B;&#x3002;</p>
</blockquote>
<h2 id="RequestMapping"><a href="#RequestMapping" class="headerlink" title="@RequestMapping"></a>@RequestMapping</h2><ul>
<li>value&#x7684;&#x503C;&#x662F;&#x6570;&#x7EC4;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x591A;&#x4E2A;url&#x6620;&#x5C04;&#x5230;&#x540C;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(value = { <span class="string">&quot;itemList&quot;</span>, <span class="string">&quot;itemListAll&quot;</span> })</span><br><span class="line">``` </span><br><span class="line">- &#x5728;<span class="class"><span class="keyword">class</span>&#x4E0A;&#x6DFB;&#x52A0;@<span class="title">RequestMapping</span>(<span class="title">url</span>)&#x6307;&#x5B9A;&#x901A;&#x7528;&#x8BF7;&#x6C42;&#x524D;&#x7F00;&#xFF0C; &#x9650;&#x5236;&#x6B64;&#x7C7B;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x65B9;&#x6CD5;&#x8BF7;&#x6C42;<span class="title">url</span>&#x5FC5;&#x987B;&#x4EE5;&#x8BF7;&#x6C42;&#x524D;&#x7F00;&#x5F00;&#x5934;</span></span><br><span class="line"><span class="class">- &#x9650;&#x5B9A;<span class="title">GET</span>&#x65B9;&#x6CD5;</span></span><br><span class="line"><span class="class">```<span class="title">java</span></span></span><br><span class="line"><span class="class">@<span class="title">RequestMapping</span>(<span class="title">value</span> </span>= <span class="string">&quot;itemList&quot;</span>,method = RequestMethod.POST)</span><br><span class="line"><span class="meta">@RequestMapping</span>(method = {RequestMethod.GET,RequestMethod.POST})</span><br></pre></td></tr></table></figure>
</li>
</ul>
<a id="more"></a>
<h2 id="Controller&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x503C;"><a href="#Controller&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x503C;" class="headerlink" title="Controller&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x503C;"></a>Controller&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x503C;</h2><h3 id="&#x8FD4;&#x56DE;ModelAndView"><a href="#&#x8FD4;&#x56DE;ModelAndView" class="headerlink" title="&#x8FD4;&#x56DE;ModelAndView"></a>&#x8FD4;&#x56DE;ModelAndView</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">&quot;/updateitem.action&quot;</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> ModelAndView <span class="title">update</span><span class="params">(Items item)</span></span>{</span><br><span class="line">	ModelAndView ma = <span class="keyword">new</span> ModelAndView();</span><br><span class="line">	im.updateItem(item);</span><br><span class="line"></span><br><span class="line">	ma.setViewName(<span class="string">&quot;success&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> ma;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="&#x8FD4;&#x56DE;String"><a href="#&#x8FD4;&#x56DE;String" class="headerlink" title="&#x8FD4;&#x56DE;String"></a>&#x8FD4;&#x56DE;String</h3><ul>
<li><p>&#x8FD4;&#x56DE;String&#x5FC5;&#x987B;&#x5728;&#x65B9;&#x6CD5;&#x7684;&#x5F62;&#x53C2;&#x4E0A;&#x7ED1;&#x5B9A;Model&#x53C2;&#x6570;</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">&quot;/itemEdit.action&quot;</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">edit</span><span class="params">(Model model , Integer id)</span></span>{</span><br><span class="line">	Items item = im.findItemsById(id);</span><br><span class="line">	model.addAttribute(<span class="string">&quot;item&quot;</span>, item);</span><br><span class="line">	<span class="keyword">return</span> <span class="string">&quot;editItem&quot;</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
<li><p>Redirect&#x91CD;&#x5B9A;&#x5411; &#x8FD4;&#x56DE;&#x5E26;redirect&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> <span class="string">&quot;redirect:/itemEdit.action?itemId=&quot;</span> + item.getId();</span><br></pre></td></tr></table></figure>
</li>
<li><p>5.3.3. forward&#x8F6C;&#x53D1;</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> <span class="string">&quot;forward: /itemEdit.action&quot;</span>;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="&#x8FD4;&#x56DE;Void"><a href="#&#x8FD4;&#x56DE;Void" class="headerlink" title="&#x8FD4;&#x56DE;Void"></a>&#x8FD4;&#x56DE;Void</h3><ul>
<li>&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x5F02;&#x6B65;&#x8BF7;&#x6C42;&#x8FD4;&#x56DE;Json&#x5B57;&#x7B26;&#x4E32;</li>
<li>&#x5728;Controller&#x65B9;&#x6CD5;&#x5F62;&#x53C2;&#x4E0A;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;request&#x548C;response&#xFF0C;&#x4F7F;&#x7528;request&#x6216;response&#x6307;&#x5B9A;&#x54CD;&#x5E94;&#x7ED3;&#x679C;</li>
</ul>
<h2 id="Json&#x6570;&#x636E;&#x4EA4;&#x4E92;"><a href="#Json&#x6570;&#x636E;&#x4EA4;&#x4E92;" class="headerlink" title="Json&#x6570;&#x636E;&#x4EA4;&#x4E92;"></a>Json&#x6570;&#x636E;&#x4EA4;&#x4E92;</h2><ul>
<li>@RequestBody&#x6CE8;&#x89E3;&#x7528;&#x4E8E;&#x8BFB;&#x53D6;http&#x8BF7;&#x6C42;&#x7684;&#x5185;&#x5BB9;(&#x5B57;&#x7B26;&#x4E32;)&#xFF0C;&#x901A;&#x8FC7;springmvc&#x63D0;&#x4F9B;&#x7684;HttpMessageConverter&#x63A5;&#x53E3;&#x5C06;&#x8BFB;&#x5230;&#x7684;&#x5185;&#x5BB9;&#xFF08;json&#x6570;&#x636E;&#xFF09;&#x8F6C;&#x6362;&#x4E3A;java&#x5BF9;&#x8C61;&#x5E76;&#x7ED1;&#x5B9A;&#x5230;Controller&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x4E0A;<h3 id="&#x52A0;&#x5165;Jackson&#x7684;JAR&#x5305;"><a href="#&#x52A0;&#x5165;Jackson&#x7684;JAR&#x5305;" class="headerlink" title="&#x52A0;&#x5165;Jackson&#x7684;JAR&#x5305;"></a>&#x52A0;&#x5165;Jackson&#x7684;JAR&#x5305;</h3><h3 id="ItemController&#x7F16;&#x5199;"><a href="#ItemController&#x7F16;&#x5199;" class="headerlink" title="ItemController&#x7F16;&#x5199;"></a>ItemController&#x7F16;&#x5199;</h3><h3 id="ResponseBody"><a href="#ResponseBody" class="headerlink" title="@ResponseBody"></a>@ResponseBody</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">&quot;testJson&quot;</span>)</span><br><span class="line"><span class="comment">// @ResponseBody</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@ResponseBody</span> <span class="function">Item <span class="title">testJson</span><span class="params">(@RequestBody Item item)</span> </span>{</span><br><span class="line">	<span class="keyword">return</span> item;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="&#x5F02;&#x5E38;&#x5904;&#x7406;"><a href="#&#x5F02;&#x5E38;&#x5904;&#x7406;" class="headerlink" title="&#x5F02;&#x5E38;&#x5904;&#x7406;"></a>&#x5F02;&#x5E38;&#x5904;&#x7406;</h2><h3 id="&#x5B9E;&#x73B0;&#x7C7B;&#x7684;&#x5199;&#x6CD5;"><a href="#&#x5B9E;&#x73B0;&#x7C7B;&#x7684;&#x5199;&#x6CD5;" class="headerlink" title="&#x5B9E;&#x73B0;&#x7C7B;&#x7684;&#x5199;&#x6CD5;"></a>&#x5B9E;&#x73B0;&#x7C7B;&#x7684;&#x5199;&#x6CD5;</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * &#x5F02;&#x5E38;&#x5904;&#x7406;&#x5668;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x5B9E;&#x73B0;&#x7C7B;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> lx</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomExceptionResolver</span> <span class="keyword">implements</span> <span class="title">HandlerExceptionResolver</span></span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> ModelAndView <span class="title">resolveException</span><span class="params">(HttpServletRequest request, </span></span></span><br><span class="line"><span class="function"><span class="params">			HttpServletResponse response, Object obj,</span></span></span><br><span class="line"><span class="function"><span class="params">			Exception e)</span> </span>{</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub  &#x53D1;&#x751F;&#x5F02;&#x5E38;&#x7684;&#x5730;&#x65B9; Serivce&#x5C42;  &#x65B9;&#x6CD5;  &#x5305;&#x540D;+&#x7C7B;&#x540D;+&#x65B9;&#x6CD5;&#x540D;&#xFF08;&#x5F62;&#x53C2;&#xFF09; &#x5B57;&#x7B26;&#x4E32;</span></span><br><span class="line">		<span class="comment">//&#x65E5;&#x5FD7;    1.&#x53D1;&#x5E03; tomcat war  Eclipse  2.&#x53D1;&#x5E03;Tomcat  &#x670D;&#x52A1;&#x5668;&#x4E0A;  Linux  Log4j</span></span><br><span class="line">		</span><br><span class="line">		ModelAndView mav = <span class="keyword">new</span> ModelAndView();</span><br><span class="line">		<span class="comment">//&#x5224;&#x65AD;&#x5F02;&#x5E38;&#x4E3A;&#x7C7B;&#x578B;</span></span><br><span class="line">		<span class="keyword">if</span>(e <span class="keyword">instanceof</span> MessageException){</span><br><span class="line">			<span class="comment">//&#x9884;&#x671F;&#x5F02;&#x5E38;</span></span><br><span class="line">			MessageException me = (MessageException)e;</span><br><span class="line">			mav.addObject(<span class="string">&quot;error&quot;</span>, me.getMsg());</span><br><span class="line">		}<span class="keyword">else</span>{</span><br><span class="line">			mav.addObject(<span class="string">&quot;error&quot;</span>, <span class="string">&quot;&#x672A;&#x77E5;&#x5F02;&#x5E38;&quot;</span>);</span><br><span class="line">		}</span><br><span class="line">		mav.setViewName(<span class="string">&quot;error&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> mav;</span><br><span class="line">	}</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="&#x5728;SpringMvc-xml&#x4E2D;"><a href="#&#x5728;SpringMvc-xml&#x4E2D;" class="headerlink" title="&#x5728;SpringMvc.xml&#x4E2D;"></a>&#x5728;SpringMvc.xml&#x4E2D;</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- &#x914D;&#x7F6E;&#x81EA;&#x5DF1;&#x5199;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x7C7B; --&gt;</span><br><span class="line">	&lt;bean class=&quot;com.joker.ssm.exception.customerExceptionResolver&quot;&gt;&lt;/bean&gt;</span><br></pre></td></tr></table></figure>
<h2 id="&#x62E6;&#x622A;&#x5668;"><a href="#&#x62E6;&#x622A;&#x5668;" class="headerlink" title="&#x62E6;&#x622A;&#x5668;"></a>&#x62E6;&#x622A;&#x5668;</h2><h3 id="&#x4E66;&#x5199;Interceptor&#x7C7B;"><a href="#&#x4E66;&#x5199;Interceptor&#x7C7B;" class="headerlink" title="&#x4E66;&#x5199;Interceptor&#x7C7B;"></a>&#x4E66;&#x5199;Interceptor&#x7C7B;</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Interceptor1</span> <span class="keyword">implements</span> <span class="title">HandlerInterceptor</span></span>{</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">preHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object arg2)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">&quot;&#x65B9;&#x6CD5;&#x524D; 1&quot;</span>);</span><br><span class="line">		<span class="comment">//&#x5224;&#x65AD;&#x7528;&#x6237;&#x662F;&#x5426;&#x767B;&#x9646;  &#x5982;&#x679C;&#x6CA1;&#x6709;&#x767B;&#x9646;  &#x91CD;&#x5B9A;&#x5411;&#x5230;&#x767B;&#x9646;&#x9875;&#x9762;   &#x4E0D;&#x653E;&#x884C;   &#x5982;&#x679C;&#x767B;&#x9646;&#x4E86;  &#x5C31;&#x653E;&#x884C;&#x4E86;</span></span><br><span class="line">		<span class="comment">// URL  http://localhost:8080/springmvc-mybatis/login.action</span></span><br><span class="line">		<span class="comment">//URI /login.action</span></span><br><span class="line">		String requestURI = request.getRequestURI();</span><br><span class="line">		<span class="keyword">if</span>(!requestURI.contains(<span class="string">&quot;/login&quot;</span>)){</span><br><span class="line">			String username = (String) request.getSession().getAttribute(<span class="string">&quot;USER_SESSION&quot;</span>);</span><br><span class="line">			<span class="keyword">if</span>(<span class="keyword">null</span> == username){</span><br><span class="line">				response.sendRedirect(request.getContextPath() + <span class="string">&quot;/login.action&quot;</span>);</span><br><span class="line">				<span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">			}</span><br><span class="line">		}</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postHandle</span><span class="params">(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)</span></span></span><br><span class="line"><span class="function">			<span class="keyword">throws</span> Exception </span>{</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">&quot;&#x65B9;&#x6CD5;&#x540E; 1&quot;</span>);</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterCompletion</span><span class="params">(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)</span></span></span><br><span class="line"><span class="function">			<span class="keyword">throws</span> Exception </span>{</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">&quot;&#x9875;&#x9762;&#x6E32;&#x67D3;&#x540E; 1&quot;</span>);</span><br><span class="line">		</span><br><span class="line">	}</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="&#x5728;sqlMapConfig-xml&#x4E2D;&#x914D;&#x7F6E;"><a href="#&#x5728;sqlMapConfig-xml&#x4E2D;&#x914D;&#x7F6E;" class="headerlink" title="&#x5728;sqlMapConfig.xml&#x4E2D;&#x914D;&#x7F6E;"></a>&#x5728;sqlMapConfig.xml&#x4E2D;&#x914D;&#x7F6E;</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- &#x914D;&#x7F6E;&#x62E6;&#x622A;&#x5668; --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:interceptors</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">mvc:interceptor</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- &#x6240;&#x6709;&#x7684;&#x8BF7;&#x6C42;&#x90FD;&#x8FDB;&#x5165;&#x62E6;&#x622A;&#x5668; --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">mvc:mapping</span> <span class="attr">path</span>=<span class="string">&quot;/**&quot;</span> /&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- &#x914D;&#x7F6E;&#x5177;&#x4F53;&#x7684;&#x62E6;&#x622A;&#x5668; --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;cn.joker.ssm.interceptor.HandlerInterceptor1&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">mvc:interceptor</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mvc:interceptors</span>&gt;</span></span><br></pre></td></tr></table></figure>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/SSM/" rel="tag"># SSM</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/06/14/ssm-1/" rel="next" title="ssm-1">
                <i class="fa fa-chevron-left"></i> ssm-1
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/06/22/E3-ONE/" rel="prev" title="E3-ONE">
                E3-ONE <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/me.jpg"
               alt="JokerLiang" />
          <p class="site-author-name" itemprop="name">JokerLiang</p>
          <p class="site-description motion-element" itemprop="description">永远年轻，永远热泪盈眶。</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">61</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">17</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/LoverJoker" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/lover-easy-7/activities" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/2292572142/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Friends
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.knight.ac.cn/" title="Knight" target="_blank">Knight</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://lzllzl.cn/" title="Zayvion" target="_blank">Zayvion</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://chen97.com/" title="Chens" target="_blank">Chens</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#RequestMapping"><span class="nav-number">1.</span> <span class="nav-text">@RequestMapping</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Controller方法返回值"><span class="nav-number">2.</span> <span class="nav-text">Controller方法返回值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#返回ModelAndView"><span class="nav-number">2.1.</span> <span class="nav-text">返回ModelAndView</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#返回String"><span class="nav-number">2.2.</span> <span class="nav-text">返回String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#返回Void"><span class="nav-number">2.3.</span> <span class="nav-text">返回Void</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Json数据交互"><span class="nav-number">3.</span> <span class="nav-text">Json数据交互</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#加入Jackson的JAR包"><span class="nav-number">3.1.</span> <span class="nav-text">加入Jackson的JAR包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ItemController编写"><span class="nav-number">3.2.</span> <span class="nav-text">ItemController编写</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ResponseBody"><span class="nav-number">3.3.</span> <span class="nav-text">@ResponseBody</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#异常处理"><span class="nav-number">4.</span> <span class="nav-text">异常处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#实现类的写法"><span class="nav-number">4.1.</span> <span class="nav-text">实现类的写法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在SpringMvc-xml中"><span class="nav-number">4.2.</span> <span class="nav-text">在SpringMvc.xml中</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#拦截器"><span class="nav-number">5.</span> <span class="nav-text">拦截器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#书写Interceptor类"><span class="nav-number">5.1.</span> <span class="nav-text">书写Interceptor类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在sqlMapConfig-xml中配置"><span class="nav-number">5.2.</span> <span class="nav-text">在sqlMapConfig.xml中配置</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">JokerLiang</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	




  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

  


</body>
</html>
